package org.widget;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.android.likeweixin.R;

/**
 * @author FuJinhu
 * 
 */
public class UrlTextView extends TextView {
	private int blue;
	private UrlClickListener urlClickListener;

	public UrlTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		blue = Color.parseColor("#ff359ed1");
		setLinkTextColor(makeColorState(R.drawable.link_color));
		setHighlightColor(Color.parseColor("#ffcccccc"));
	}

	public void setUrlText(List<TextUrl> urls) {
		if (urls == null)
			return;
		StringBuilder sb = new StringBuilder();
		for (TextUrl url : urls) {
			sb.append("<a href='" + url.key + "'>");
			sb.append(url.value);
			sb.append("</a>");
			if (urls.indexOf(url) != urls.size() - 1) {
				sb.append(" ");
			}
		}
		setText(Html.fromHtml(sb.toString()));
		setMovementMethod(LinkMovementMethod.getInstance());
		CharSequence text = getText();
		Spannable spannable = (Spannable) getText();
		URLSpan[] urlspan = spannable.getSpans(0, text.length(), URLSpan.class);
		SpannableStringBuilder stylesBuilder = new SpannableStringBuilder(text);
		stylesBuilder.clearSpans(); // should clear old spans
		for (URLSpan url : urlspan) {
			TextViewURLSpan myURLSpan = new TextViewURLSpan(url.getURL());
			stylesBuilder.setSpan(myURLSpan, spannable.getSpanStart(url),
					spannable.getSpanEnd(url),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		setText(stylesBuilder);
	}

	private ColorStateList makeColorState(int color) {
		ColorStateList csl = null;
		XmlResourceParser xppcolor = getResources().getXml(color);
		try {
			csl = ColorStateList.createFromXml(getResources(), xppcolor);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return csl;
	}

	private SpannableString makeDrawableSpan(int res) {
		Drawable drawable = getResources().getDrawable(res);

		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());

		SpannableString spannableString = new SpannableString("pics");

		ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

		spannableString.setSpan(imageSpan, 0, spannableString.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return spannableString;
	}

	public void setUrlClickListener(UrlClickListener listener) {
		this.urlClickListener = listener;
	}

	private class TextViewURLSpan extends ClickableSpan {
		private String clickString;

		public TextViewURLSpan(String clickString) {
			this.clickString = clickString;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			ds.setColor(blue);
			ds.setUnderlineText(false); // 去掉下划线
		}

		@Override
		public void onClick(View widget) {
			System.out.println("clickString:" + clickString);
			if (urlClickListener != null) {
				urlClickListener.onClick(clickString);
			}
		}
	}

	public static final class TextUrl {
		public String key;
		public String value;

		public TextUrl() {

		}

		public TextUrl(String key, String value) {
			this.key = key;
			this.value = value;
		}
	}

	public static interface UrlClickListener {
		public void onClick(String key);
	}
}
